//
//  CouponListViewController.m
//  BP_NewClient
//
//  Created by letv on 15/8/23.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "CouponListViewController.h"
#import "CouponTableViewCell.h"
#import "NSString+ChatRoom.h"
@interface CouponListViewController ()

@end

@implementation CouponListViewController
@synthesize orderModel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK(@selector(back:));
    self.navigationItem.title = @"优惠券";
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - kTabBarHeight- 20) collectionViewLayout:flowLayOut];
    collectView.backgroundColor = [UIColor clearColor];
    collectView.delegate = self;
    collectView.dataSource = self;
    [self.view addSubview:collectView];
    
    [collectView registerNib:[UINib nibWithNibName:@"CouponTableViewCell" bundle:nil] forCellWithReuseIdentifier:@"CouponTableViewCell"];
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.orderModel.coupon_list.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CouponTableViewCell";
    CouponTableViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    CouponModel *model = self.orderModel.coupon_list[indexPath.row];
    
    cell.info.text = model.coupon_info;
    cell.price.text = [NSString stringWithFormat:@"%@元",model.coupon_prize];
    cell.timeStamp.text = [NSString stringWithFormat:@"有效期至:%@",[NSString convertDateyHd:model.time]];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth - 20 , 120);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor whiteColor];
//    
//    Nav_MainViewController *nav_Main = [[Nav_MainViewController alloc] initWithNibName:@"Nav_MainViewController" bundle:nil];
//    [self.navigationController pushViewController:nav_Main animated:YES];
    
    CouponModel *model = OBJECT_OF_ATINDEX(self.orderModel.coupon_list, indexPath.row);
    if (_couponBlock) {
        _couponBlock(model);
        [self back:nil];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
